#include "disclaimerdialog.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextBrowser>

DisclaimerDialog::DisclaimerDialog(bool interaction, QWidget* parent) : QDialog { parent }
{
    setWindowTitle(tr("YingCe Disclaimer"));
    resize(800, interaction ? 680 : 640);

    QVBoxLayout* vl = new QVBoxLayout(this);
    vl->setContentsMargins(16, 16, 16, 16);
    vl->setSpacing(10);

    QTextBrowser* browser = new QTextBrowser(this);
    vl->addWidget(browser, 1);

    QDir res_dir(QCoreApplication::applicationDirPath());
#ifdef Q_OS_MAC
    res_dir.cdUp();
#endif
    res_dir.cd("Resources");

    QFile file(res_dir.absoluteFilePath("disclaimer.html"));
    if (file.open(QFile::ReadOnly)) {
        const auto ba = file.readAll();
        file.close();
        browser->setHtml(ba);
    }

    if (interaction) {
        QHBoxLayout* hl = new QHBoxLayout;
        hl->setContentsMargins(0, 0, 0, 0);
        hl->setSpacing(10);
        vl->addLayout(hl);
        hl->addStretch(1);

        QPushButton* accept_button = new QPushButton(tr("Accept"), this);
        accept_button->setObjectName("accept");
        hl->addWidget(accept_button);

        QPushButton* reject_button = new QPushButton(tr("Reject"), this);
        reject_button->setObjectName("reject");
        hl->addWidget(reject_button);

        connect(accept_button, &QPushButton::clicked, this, &QDialog::accept);
        connect(reject_button, &QPushButton::clicked, this, &QDialog::reject);
    }
}
